using System;
using System.IO;

namespace CodePlex.CodePlexClientLibrary
{
    [Command("commit", "Commits your changes to the source control server.", AltName = "ci", MaxArgs = 1, UsageSummary = "[directory] [options]",
        UsageDescription = @"If /message is not provided, an external editor is launched to edit the
commit message. The default editor is notepad.exe, which can be overridden
with /editor or in the .exe.config file.

When providing a message with spaces in it, surround the message with quote
characters ("""").")]
    public class CommitCommand : AuthenticatedCommand
    {
        string editor;
        bool? gui;
        string message;

        public CommitCommand()
        {
            TryAnonymous = false;
        }

        [Option("editor", "external editor command")]
        public string Editor
        {
            get { return editor ?? Config.Editor; }
            set { editor = value; }
        }

        [Option("gui", "launch (don't launch) commit GUI", ShortName = "g", ShowPlusMinus = true)]
        public bool Gui
        {
            get { return gui.HasValue ? gui.Value : Config.DefaultToGuiForCommit; }
            set { gui = value; }
        }

        [Option("message", "commit message (surround text with \"\" for spaces)", ShortName = "m")]
        public string Message
        {
            get { return message; }
            set { message = value; }
        }

        public override void Execute()
        {
            string localPath;

            if (Arguments.Count == 0)
                localPath = Directory.GetCurrentDirectory();
            else
                localPath = CodePlexClientUtil.GetFullPath(Arguments[0]);

            if (Gui && NonInteractive)
            {
                Console.WriteLine("error: /gui and /non-interactive are mutually exclusive");
                return;
            }

            if (Message == null && NonInteractive)
            {
                Console.WriteLine("error: You must provide specify /message with /non-interactive");
                return;
            }

            ICommitView view;

            if (Gui)
                view = new CommitGuiView(Engine, Message, localPath, Config.DiffTool, Config.DiffArgs,
                                         Config.MergeTool, Config.MergeArgs);
            else
                view = new CommitConsoleView(Engine, Editor, Message, localPath);

            view.Run();
        }
    }
}